package com.hk.rabbitmq.controller;

import com.hk.rabbitmq.config.ConfirmConfig;
import com.hk.rabbitmq.config.MyCallBack;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.Date;


/**
 * @author: dpb
 * @date: 2022/3/29
 */
@RestController
@Slf4j
@RequestMapping("/confirm")
@Api(value = "/confirm",tags = "RabbitMQ接口")
public class ProducerController {


    public static final String QUEUENAME = "chatbot";

    @Autowired
    private RabbitTemplate rabbitTemplate;


//    @Autowired
//    MyCallBack myCallBack;

    //由于MyCallBack为一个bean，得注入到RabbitTemplate才能使用
//    @PostConstruct
//    public void init(){
//        rabbitTemplate.setConfirmCallback(myCallBack);
//    }


    //发消息，测试发布确认
    @GetMapping("sendMessage/{message}")
    @ApiOperation("发送消息")
    @ApiResponses({
            @ApiResponse(code= 200,message = "返回成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "message", value = "消息", required = true)
    })
    public void sendMessage(@PathVariable("message") String message){
        String context = "hello " + new Date();
        log.info("发送的消息为 : {}", context);
//        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME,ConfirmConfig.CONFIRM_ROUTINGKEY,message);
        rabbitTemplate.convertAndSend(QUEUENAME, context);
    }


}
